package com.samphanie.auiu.app.entity.dto;

import com.samphanie.auiu.app.entity.User;
import lombok.*;

import java.io.Serializable;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Data
@With
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserDto implements Serializable {
    private static final long serialVersionUID = -937816877433199485L;

    private Long id;
    private String username;
    private String nickname;
    private String mobile;
    private String email;
    private boolean enabled;
    private boolean usingMfa;
    private Set<RoleDto> roles;

    public static Function<User, UserDto> fromUser = (user) -> UserDto.builder()
            .id(user.getId())
            .username(user.getUsername())
            .nickname(user.getNickname())
            .email(user.getEmail())
            .mobile(user.getMobile())
            .enabled(user.isEnabled())
            // .usingMfa(user.isUsingMfa())
            .roles(user.getRoles().stream()
                    .map(role -> RoleDto.builder()
                            .id(role.getId())
                            .roleName(role.getRoleName())
                            .displayName(role.getDisplayName())
                            .builtIn(role.isBuiltIn())
                            .build())
                    .collect(Collectors.toSet()))
            .build();
}
